package com.mzy.linear_struct.subject;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: codermzy
 * @Date: 2024/03/30/12:18
 * @Description:
 */
public class 反转链表 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    static class Solution {
        // public ListNode reverseList(ListNode head) {
        //     ListNode cur = head;
        //     ListNode prev = null;
        //     ListNode next = null;
        //     while (cur != null) {
        //         next = cur.next;
        //         cur.next = prev;
        //         prev = cur;
        //         cur = next;
        //     }
        //     return prev;
        // }


        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) return head;

            ListNode newHead = reverseList(head.next);

            head.next.next = head;
            head.next = null;
            return newHead;
        }
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node1.next = node2;
        node2.next = node3;
        ListNode head = node1;
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }

        head = new Solution().reverseList(node1);
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }
}
